import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:detect/utils/global.dart';

class ToastUtil {
  ToastUtil._();

  static Future<void> close() async => SmartDialog.dismiss(status: SmartStatus.toast);

  static Future<void> closeAll() async => SmartDialog.dismiss(status: SmartStatus.allToast);

  static Future<void> msg(String msg, {Duration? displayTime, Duration? animationTime}) => _showToast(
        msg,
        displayTime: displayTime,
        animationTime: animationTime,
      );

  static Future<void> success(String msg, {Duration? displayTime, Duration? animationTime}) =>
      icon(Icon(Icons.check_circle_outline, size: Global.ds(50)), msg, displayTime: displayTime, animationTime: animationTime);

  static Future<void> failure(String msg, {Duration? displayTime, Duration? animationTime}) =>
      icon(Icon(Icons.error_outline, size: Global.ds(50)), msg, displayTime: displayTime, animationTime: animationTime);

  static Future<void> error(String msg, {Duration? displayTime, Duration? animationTime}) =>
      icon(Icon(Icons.highlight_off, size: Global.ds(50)), msg, displayTime: displayTime, animationTime: animationTime);

  static Future<void> icon(Widget icon, String msg, {Duration? displayTime, Duration? animationTime}) => _showToast(
        msg,
        icon: icon,
        displayTime: displayTime,
        animationTime: animationTime,
      );

  static Future<void> _showToast(String msg, {Widget? icon, Duration? displayTime, Duration? animationTime}) {
    SmartDialog.config.toast = SmartConfigToast(
      intervalTime: Duration.zero,
      alignment: Alignment.center,
      displayType: SmartToastType.last,
    );
    return SmartDialog.showToast(
      msg,
      displayTime: displayTime,
      animationTime: animationTime,
      useAnimation: animationTime != Duration.zero,
      builder: (context) => _createNoticeWidget(msg, icon),
    );
  }

  static Widget _createNoticeWidget(String label, [Widget? icon]) => ConstrainedBox(
        key: UniqueKey(),
        constraints: BoxConstraints(maxWidth: Global.ds(480), maxHeight: Global.ds(480)),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Flexible(
              child: Container(
                clipBehavior: Clip.antiAlias,
                padding: EdgeInsets.symmetric(horizontal: Global.ds(30), vertical: Global.ds(20)),
                decoration: BoxDecoration(color: Colors.black87, borderRadius: BorderRadius.circular(Global.ds(4))),
                child: icon == null
                    ? Text(
                        label,
                        softWrap: true,
                        maxLines: 5,
                        overflow: TextOverflow.ellipsis,
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: Global.ds(18), color: Colors.white),
                      )
                    : Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          icon,
                          Divider(height: Global.ds(5), color: Colors.transparent),
                          Text(
                            label,
                            softWrap: true,
                            maxLines: 5,
                            overflow: TextOverflow.ellipsis,
                            textAlign: TextAlign.center,
                            style: TextStyle(fontSize: Global.ds(18), color: Colors.white),
                          ),
                        ],
                      ),
              ),
            ),
          ],
        ),
      );
}
